package com.hm.ruiji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hm.ruiji.common.CustomException;
import com.hm.ruiji.dto.DishDto;
import com.hm.ruiji.entity.Dish;
import com.hm.ruiji.entity.DishFlavor;
import com.hm.ruiji.mapper.DishMapper;
import com.hm.ruiji.service.DishFlavorService;
import com.hm.ruiji.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Resource
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional // 开启事务方法在执行时报错会进行回滚
    public void saveWithFlavor(DishDto dishDto){

        //  保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        //  给菜品的口味添加dishId
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //  保存菜品口味数据倒dishflavor
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //  根据id获取菜品基本信息
        Dish dish = this.getById(id);

        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        //  根据菜品id查询口味信息
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(wrapper);

        //  创建DTO对象存储菜品信息和口味信息
        DishDto dishDto = new DishDto();
        /**
         * 拷贝对象
         * Params:
         * source – the source bean
         * target – the target bean
         */
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 修改菜品信息
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //  更新基本菜品信息
        this.updateById(dishDto);
        //  删除口味信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(wrapper);
        //  重新添加口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        //  给口味信息添加菜品id
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        //  保存菜品口味数据倒dishflavor
        dishFlavorService.saveBatch(flavors);
    }

    //@Override
    //public void removeByIdsWithFlavor(List<Long> ids) {
    //    //  根据菜品id批量删除菜品基础信息
    //    this.removeByIds(ids);
    //    //  根据菜品id查询口味id
    //    QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
    //    wrapper.select("id").in("dish_id", ids);
    //    List<DishFlavor> flavorList = dishFlavorService.list(wrapper);
    //    log.info("查询到的菜品信息" + flavorList.toString());
    //    //  将口味id存入list
    //    List<Long> dishFlavorId =  new ArrayList();
    //    for (DishFlavor dishFlavor : flavorList) {
    //        dishFlavorId.add(dishFlavor.getId());
    //    }
    //    //  批量删除口味信息
    //    dishFlavorService.removeByIds(dishFlavorId);
    //}

    /**
     * 删除菜品和口味信息
     * @param ids
     */
    @Override
    public boolean removeWithFlavor(List<Long> ids) {
        // 删除菜品基本信息
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.in(Dish::getId,ids);
        dishWrapper.eq(Dish::getStatus, 1); // 删除停售的    0停售 1在售
        int count = this.count(dishWrapper);

        if (count > 0) {
            log.error("你选中的菜品中包含在售产品，请停售后再删除");
            return false;
        }

        this.removeByIds(ids);

        // 删除套餐菜品关系信息
        LambdaQueryWrapper<DishFlavor> flavorWrapper = new LambdaQueryWrapper<>();
        flavorWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(flavorWrapper);
        return true;
    }
}
